﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Samples.ServiceHosting.StorageClient;
using System.IO;

namespace Astoman.Common.Repository
{
    public class BlobRepository
    {
        private BlobStorage _bs = null;

        public BlobRepository(StorageAccountInfo accountInfo)
        {
            _bs = BlobStorage.Create(accountInfo);
        }

        #region Container methods

        public List<string> GetContainerNames()
        {
            var containers = _bs.ListBlobContainers();
            return (from cont in containers
                    select cont.ContainerName).ToList();
        }

        public void DeleteContainer(string containerName)
        {
            var container = _bs.GetBlobContainer(containerName);
            container.DeleteContainer();
        }

        public bool SaveContainerToDisk(String containerName, String savePath)
        {
            var blobs = GetBlobPropertiesInContainer(containerName);
            bool isContainerEmpty = blobs.Count() == 0;
            if (isContainerEmpty) return false;

            var container = _bs.GetBlobContainer(containerName);
            foreach (var blob in blobs)
            {
                this.SaveBlobToDisk(container, blob.Name, Path.Combine(savePath, containerName));
            }
            return true;
        } 
        
        public int CountContainers()
        {
            var containers = _bs.ListBlobContainers();
            return containers.Count();
        }

        public ContainerProperties GetContainerProperties(String containerName)
        {
            var container = _bs.GetBlobContainer(containerName);
            return container.GetContainerProperties();
        }

        #endregion

        #region Blob methods

        public void SaveBlobToDisk(String containerName, String blobName, String savePath)
        {
            var container = _bs.GetBlobContainer(containerName);
            this.SaveBlobToDisk(container, blobName, savePath);
        }

        private void SaveBlobToDisk(BlobContainer container, String blobName, String savePath)
        {
            /*Create directory structure*/
            var fullPath = Path.Combine(savePath, blobName);
            var dirStructure = Path.GetDirectoryName(fullPath);
            Directory.CreateDirectory(dirStructure);

            /*Get blob and write to file*/
            using (var filestream = File.Create(fullPath))
            {
                BlobContents contents = new BlobContents(filestream);
                var property = container.GetBlob(blobName, contents, false);
            }
        }

        public IEnumerable<BlobProperties> GetBlobPropertiesInContainer(String containerName)
        {
            var container = _bs.GetBlobContainer(containerName);
            return from blopProp in container.ListBlobs("", false)
                   select blopProp as BlobProperties;
        }

        public BlobProperties GetBlobProperties(String containerName, String blobName)
        {
            var container = _bs.GetBlobContainer(containerName);
            return container.GetBlobProperties(blobName);
        }

        public int CountAllBlobs()
        {
            var containerNames = this.GetContainerNames();
            int count = 0;
            foreach (var containerName in containerNames)
            {
                count += GetBlobPropertiesInContainer(containerName).Count();
            }
            return count;
        }

        public void DeleteBlob(String containerName, String blobName)
        {
            var container = _bs.GetBlobContainer(containerName);
            container.DeleteBlob(blobName);
        }

        #endregion
    }
}
